﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System.Data;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Web.Support;


namespace SIPO.Web.Forms.Transaction
{
    public partial class recInvoiceManualform : BasePage
    {
        private string id = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (Request.Params["Id"] != null)
            {
                id = Request.Params["Id"].ToString();
            }

            if (!IsPostBack)
            {

                recTransactionTypeRepo recTransactionTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                //populate dropdownlist transactionType
                recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recTransactionType> transTypes = recTransactionTypeDal.GetListIsBilling();
                recSubSectorRepo recSubSectordal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recSubSubSectorRepo recSubSubSectordal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                ddlJenisTransaksi.Items.Clear();
                if (transTypes.Count > 0)
                {
                    transTypes.ForEach(delegate(recTransactionType transType)
                    {
                        ddlJenisTransaksi.Items.Add(new ListItem(transType.TransTypeCode + "-" + transType.TransTypeName, transType.TransTypeID.ToString()));
                    });
                }
                ddlJenisTransaksi.Items.Insert(0, new ListItem("-- pilih jenis transaksi --", string.Empty));

                if (id != string.Empty) // Edit an existing item
                {
                    //ddlJenisTransaksi.Focus();
                    recBillingRepo recBillingDal = new recBillingRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recDebtorSectorRepo recDebtorSectorDal=new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recBilling Billing = recBillingDal.GetById(id.ToString());
                    recDebtorSector Dbs = recDebtorSectorDal.GetById(Billing.DebtorSectorID);

                    List<recSubSector> SubSector = recSubSectordal.GetListByDebtorID_AND_subsubsectorIDs(Dbs.DebtorID, base.SubSubSectorIDs);
                    ddlSubSector.Items.Clear();
                    if (SubSector.Count > 0)
                    {
                        SubSector.ForEach(delegate(recSubSector subsektor)
                        {
                            ddlSubSector.Items.Add(new ListItem(subsektor.SubSectorCode+"-"+subsektor.SubSectorName,subsektor.SubSectorID.ToString()));
                        });
                    }
                    List<recSubSubSector> SubSubSector = recSubSubSectordal.GetListByDebtorID_AND_SubSubSectorIDs(Dbs.DebtorID,base.SubSubSectorIDs);
                    ddlSubSubSector.Items.Clear();
                    if (SubSubSector.Count > 0)
                    {
                        SubSubSector.ForEach(delegate(recSubSubSector subsubsektor)
                        {
                            ddlSubSubSector.Items.Add(new ListItem(subsubsektor.SubSubSectorCode+"-"+subsubsektor.SubSubSectorName,subsubsektor.SubSubSectorID.ToString()));
                        });
                    }
                    List<recDebtor> Debtor = recDebtorDal.GetList(base.SubSubSectorIDs);
                    ddlDebtor.Items.Clear();
                    if (Debtor.Count > 0)
                    {
                        Debtor.ForEach(delegate(recDebtor debtor)
                        {
                            ddlDebtor.Items.Add(new ListItem(debtor.DebtorCode + "-" + debtor.DebtorName, debtor.DebtorID.ToString()));
                        });
                    }
                    ddlJenisTransaksi.SelectedValue = Billing.TransTypeID.ToString();
                    txtValue.Text = Billing.Value.ToString();
                    txtKeterangan.Text = Billing.ForPayment;
                    ddlDebtor.SelectedValue = Dbs.DebtorID.ToString();
                    ddlSubSector.SelectedValue = Dbs.SubSectorID.ToString();
                    ddlSubSubSector.SelectedValue = Dbs.SubSubSectorID.ToString();
                    hfID.Value = id.ToString();
                    hfDueDate.Value = Billing.DueDate.ToString();
                    hfBillDate.Value = Billing.BillingDate.ToString();
                }

                  }
        }
    }
}